<?php

/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property integer $article_id
 * @property integer $user_id
 * @property string $title
 * @property string $content
 * @property integer $cate_id
 * @property string $datetime
 * @property integer $top
 * @property integer $digest
 * @property integer $status
 * @property integer $sort
 * @property integer $viewcount
 * @property string $create_time
 * @property string $update_time
 * @property integer $is_delete
 * @property integer $tags
 *
 * The followings are the available model relations:
 */
class article extends CActiveRecord
{
	private $_oldTags;
	/**
	 * Returns the static model of the specified AR class.
	 * @return article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, content, cate_id, datetime', 'required'),
			array('user_id, cate_id, top, digest, status, sort, viewcount, is_delete', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>255),
			array('update_time', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			
			array('tags', 'normalizeTags'),

			array('article_id, user_id, title, content, cate_id, datetime, top, digest, status, sort, viewcount, create_time, update_time, is_delete', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(			
			'article_id' => Yii::t('admin','Article_ID'),
			'user_id' => Yii::t('admin','User'),
			'title' => Yii::t('admin','Title'),
			'content' => Yii::t('admin','Content'),
			'cate_id' => Yii::t('admin','Cate'),
			'datetime' => Yii::t('admin','Datetime'),
			'top' => Yii::t('admin','Top'),
			'digest' => Yii::t('admin','Digest'),
			'status' => Yii::t('admin','Status'),
			'sort' => Yii::t('admin','Sort'),
			'viewcount' => Yii::t('admin','Viewcount'),
			'create_time' => Yii::t('admin','Create Time'),
			'update_time' => Yii::t('admin','Update Time'),
			'is_delete' => Yii::t('admin','Is Delete'),
			'tags' => Yii::t('admin','Tags'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('article_id',$this->article_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('cate_id',$this->cate_id);
		$criteria->compare('datetime',$this->datetime,true);
		$criteria->compare('top',$this->top);
		$criteria->compare('digest',$this->digest);
		$criteria->compare('status',$this->status);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('viewcount',$this->viewcount);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('is_delete',$this->is_delete);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'article_id DESC'),
		));
	}
	
/**
	 * Normalizes the user-entered tags.
	 */
	public function normalizeTags($attribute,$params)
	{
		$this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags)));
	}
	
/**
	 * @return array a list of links that point to the post list filtered by every tag of this post
	 */
	public function getTagLinks()
	{
		$links=array();
		foreach(Tag::string2array($this->tags) as $tag)
			$links[]=CHtml::link(CHtml::encode($tag), array('article/index', 'tag'=>$tag));
		return $links;
	}
	
	/**
	 * This is invoked when a record is populated with data from a find() call.
	 */
	protected function afterFind()
	{
		parent::afterFind();
		$this->_oldTags=$this->tags;
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->create_time=$this->update_time=date("Y-m-d H-m-s");
				$this->user_id=Yii::app()->user->id;
			}
			else
			{
				$this->update_time=date("Y-m-d H-m-s");
			}
			return true;
		}
		else
			return false; 
	}

	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
		Tag::model()->updateFrequency($this->_oldTags, $this->tags);
	}

	/**
	 * This is invoked after the record is deleted.
	 */
	protected function afterDelete()
	{
		parent::afterDelete();
		Tag::model()->updateFrequency($this->tags, '');
	}
	
}